package
{
	import alttackEvents.AlttackEvent;
	
	import com.greensock.easing.Cubic;
	import com.greensock.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	import model.Image;
	
	import view.GameData;
	import view.GameSelect;
	import view.PigButton;
	import view.Title;
	import view.Toolbar;

	[SWF(width="600",height="800",frameRate="60")]
	public class BravadaDemo extends Sprite
	{
		
		/**
		 *变量 
		 */		
		//背景
		public var subject:Image;
		//标题
		public var title:Title;
		//选择器
		public var gameIcon:GameSelect;
		//信息版
		public var gamefiles:GameData;
		//工具栏
		public var toolbar:Toolbar;
		//标题栏的文字
		public var pigbutton:PigButton;
		
		public var gameName:String;

		//主遮罩
		private var shape:Shape;
		
		
		public function BravadaDemo()
		{
			//对齐和缩放
			stage.align = StageAlign.TOP_LEFT
			stage.scaleMode = StageScaleMode.SHOW_ALL;
			
			
			//主背景
			subject = new Image();
			this.addChild(subject);			
			drawMask();
			//this.mask = shape;
			
			mainInit()
			
			
		}
		
		private function mainInit():void
		{
			subject.url = "assets/底图.png";
			addTitle();
			chooser();
			
		}
		
		
		//----------------------------------
		/**
		 * 选择器
		 * 
		 */		
				
		private  function chooser():void
		{
			gameIcon = new GameSelect();
			addChild(gameIcon);
			gameIcon.addEventListener(GameSelect.BOARD,notice);	
			
		}				
		//删除
		private  function chooserRemove():void
		{
			if(gameIcon)
			{
				gameIcon.removeEventListener(GameSelect.BOARD,notice);	
				gameIcon.meHide();
				gameIcon=null;
			}
		}
		
		
		
		
		
		
		//---------------------------------------------------------	
		/**
		 * 信息面板
		 * @param event
		 * 
		 */
		
		protected function notice(event:Event=null):void
		{
			if(!gamefiles && gameIcon.urlIcon == "assets/图标猪2.swf"){
				gamefiles = new GameData();
				addChild(gamefiles);
				gamefiles.addEventListener(GameData.GAME_START,onGamestart);
			}
						
		}
		//删除
		protected function noticeRemove():void
		{
			if(gamefiles)
			{
				gamefiles.removeEventListener(GameData.GAME_START,onGamestart);
				gamefiles.meHide();
				gamefiles = null;
			}						
		}
		
		
		
		
		//-------------------------------------------------
		/**
		 * 大炮和工具栏<p></p>
		 * toolbar需要放在最顶层，否则点击攻击可能失效
		 */
		
		private function cannonInit():void
		{
			toolbar = new Toolbar();
			toolbar.addEventListener(AlttackEvent.ALTTACK,onAlttackEvent);
			addChild(toolbar);			
		}					
		
		protected function onAlttackEvent(event:Event):void
		{
			// TODO Auto-generated method stub
			
		}
		//删除
		private function toolbarRemove():void
		{
			if(toolbar){
				toolbar.meHide();
				toolbar = null;
			}
			
		}			
		
		 
		//---------------------------------------------------------	
		/**
		 *pig游戏标题上的按钮
		 * 有三个按钮；<></p>
		 * 暂停/开始、返回
		 * 其中一个是text可以写入数字,表示所处关卡。 
		 * 
		 */		
		private function addPigButt():void
		{
			pigbutton = new PigButton();
			addChild(pigbutton);
			pigbutton.addEventListener(Image.COMPLETE,onPigButt);
		}		
		protected function onPigButt(event:Event):void
		{
			pigbutton.removeEventListener(Image.COMPLETE,onPigButt);
			pigbutton.addEventListener(PigButton.CLICK_EVENT,onPigSuspend);
			
		}		
		protected function onPigSuspend(event:Event):void
		{
			switch(pigbutton.buttonClidk){
				case pigbutton.stopButt:
					toolbar.trigger = false;
					break;
				case pigbutton.startButt:
					toolbar.trigger = true;
					break
				case pigbutton.getbackButt:
					resetGame();	
					break;
			}						
		}		
		//删除方法
		private function addPigButtRemove():void
		{					
			pigbutton.addEventListener(PigButton.CLICK_EVENT,onPigSuspend);
			removeChild(pigbutton);
			pigbutton = null;
		}
				
		
		
		//---------------------------------------------------------		
		/**
		 * 标题
		 * 
		 */		
		private function addTitle():void
		{
			title = new Title();
			this.addChild(title);
			//title.addEventListener(Image.COMPLETE,ontitle)
		}
		protected function ontitle(event:Event):void
		{
			title.removeEventListener(Image.COMPLETE,ontitle)
			title.x = 0;
			title.y = -title.height;
			TweenLite.to(title, 0.3, {y:0, ease:Cubic.easeOut});			
		}
		//删除
		private function titleRemove():void
		{
			if(title){
				title.meHide();
				title = null;
			}
			
		}
		
		
		//-------------------------------------------------
		//pig游戏初始化
		protected function onGamestart(event:Event):void
		{
			addPigButt();
			cannonInit();
			subject.url = "assets/shamo.png";
			chooserRemove();
			noticeRemove();						
			
		}
		
		/**
		 *pig返回主界面 
		 * 
		 */		
		private function resetGame():void
		{
			addPigButtRemove();
			toolbarRemove();
			subject.url = "assets/底图.png";
			chooser();
			notice();
		}
		
		
		//画遮罩
		private function drawMask():void
		{
			shape = new Shape();
			shape.graphics.beginFill(0xffffff);
			shape.graphics.drawRect(0,0,600,800);
			shape.graphics.endFill();
			
		}				
				

	}
}